package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/admin/product")
//@CrossOrigin//解决跨域问题  添加好网关以后不再使用
public class ManageController {

    @Autowired
    private ManageService manageService;

    //查询一级分类集合
    @GetMapping("/getCategory1")
    public Result getCategory1(){

        List<BaseCategory1> baseCategory1List =  manageService.getCategory1();

        return Result.ok(baseCategory1List);
    }

    //查询二级分类集合
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id){

        List<BaseCategory2> baseCategory2List = manageService.getCategory2(category1Id);

        return Result.ok(baseCategory2List);
    }

    //查询三级分类集合
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id){

        List<BaseCategory3> baseCategory3List = manageService.getCategory3(category2Id);

        return Result.ok(baseCategory3List);
    }

    //根据id获取平台属性
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(
            @PathVariable Long category1Id,
            @PathVariable Long category2Id,
            @PathVariable Long category3Id
            ){
        List<BaseAttrInfo> baseAttrInfoList = manageService.attrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(baseAttrInfoList);
    }

    //添加或修改平台属性,传的是BaseAttrInfo对象
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        //从前台传过来的数据都被封装到该对象中
        manageService.saveAttrInfo(baseAttrInfo);

        return Result.ok();
    }

    //根据平台属性id获取平台属性
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){
        //根据attrId获取BaseAttrInfo对象
        BaseAttrInfo baseAttrInfo = manageService.getAttrInfo(attrId);
        //根据BaseAttrInfo对象获取BaseAttrValue集合
        List<BaseAttrValue> baseAttrValueList = baseAttrInfo.getAttrValueList();
        return Result.ok(baseAttrValueList);
    }

    //获取品牌分页列表
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result baseTrademark(@PathVariable Integer page,@PathVariable Integer limit){

        IPage ipage = manageService.baseTrademark(page,limit);

        return Result.ok(ipage);
    }

}
